﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Singer
{
    public partial class SingerManage : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        string keyword;
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            if (txtKeysearch.Text != "")
                keyword = txtKeysearch.Text.Trim();
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize30 + 1;
            if (from == 1) from = 0;
            int to = (cPage) * Const.pageSize30;
            totalRecords = SingerBL.GetCount(keyword, Const.show);
            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize30;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "keyword", "from", "to", "cPage" },
                                    new string[] { keyword, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "keyword", "from", "to" },
                                    new string[] { keyword, from.ToString(), to.ToString() });
            CHRTList<SingerInfo> list = SingerBL.SearchAll(keyword, Const.show, from, to);
            if (list != null && list.Count > 0)
            {
                grdSinger.Visible = true;
                grdSinger.DataSource = list;
                grdSinger.RowDataBound += new GridViewRowEventHandler(grdSinger_RowDataBound);
                grdSinger.DataBind();
                PagingAjax1.Visible = true;
            }
            else
            {
                grdSinger.Visible = false;
                lblMessage.Text = "Không tồn tại dữ liệu !";
                PagingAjax1.Visible = false;
            }
        }
        #endregion

        #region RowDataBound
        void grdSinger_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton img = (ImageButton)e.Row.FindControl("img");
                SingerInfo info = (SingerInfo)e.Row.DataItem;

                e.Row.Cells[0].Text = (Const.pageSize30 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
                if (info.Images.Length > 0)
                {
                    img.Visible = true;
                    img.ImageUrl = info.Images;
                }
            }
        }
        #endregion

        #region RowCommand
        public void grdSinger_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int singerId = Convert.ToInt32(e.CommandArgument);
                            SingerBL.Delete(singerId);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        #endregion




    }
}